@extends('admin.layouts.app')

@section('title', '系统设置详情')

@section('content')
<div class="container-fluid">
    <!-- 页面标题 -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-0 text-gray-800">系统设置详情</h1>
            <p class="mb-0 text-muted">查看系统配置参数的详细信息</p>
        </div>
        <div>
            <a href="{{ route('admin.system-settings.edit', $setting) }}" class="btn btn-primary btn-sm me-2">
                <i class="fas fa-edit"></i> 编辑设置
            </a>
            <a href="{{ route('admin.system-settings.index') }}" class="btn btn-outline-secondary btn-sm">
                <i class="fas fa-arrow-left"></i> 返回列表
            </a>
        </div>
    </div>

    <div class="row">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <h6 class="card-title mb-0">基本信息</h6>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label text-muted">设置键名</label>
                                <div class="form-control-plaintext">
                                    <code>{{ $setting->key }}</code>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label text-muted">显示名称</label>
                                <div class="form-control-plaintext">
                                    {{ $setting->label }}
                                    @if($setting->is_required)
                                        <span class="badge bg-danger ms-2">必填</span>
                                    @endif
                                    @if($setting->is_public)
                                        <span class="badge bg-info ms-1">公开</span>
                                    @endif
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label text-muted">设置分组</label>
                                <div class="form-control-plaintext">
                                    <span class="badge bg-primary">
                                        <i class="fas fa-{{ $setting->group === 'general' ? 'cog' : ($setting->group === 'email' ? 'envelope' : ($setting->group === 'security' ? 'shield-alt' : ($setting->group === 'interface' ? 'palette' : ($setting->group === 'upload' ? 'cloud-upload-alt' : 'server')))) }} me-1"></i>
                                        {{ $groups[$setting->group] ?? $setting->group }}
                                    </span>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label text-muted">数据类型</label>
                                <div class="form-control-plaintext">
                                    <span class="badge bg-secondary">{{ $types[$setting->type] ?? $setting->type }}</span>
                                </div>
                            </div>
                        </div>
                    </div>

                    @if($setting->description)
                        <div class="mb-3">
                            <label class="form-label text-muted">描述说明</label>
                            <div class="form-control-plaintext">
                                {{ $setting->description }}
                            </div>
                        </div>
                    @endif

                    <div class="mb-3">
                        <label class="form-label text-muted">当前值</label>
                        <div class="form-control-plaintext">
                            @if($setting->type === 'boolean')
                                <div class="form-check form-switch">
                                    <input class="form-check-input" type="checkbox" disabled 
                                           {{ $setting->value ? 'checked' : '' }}>
                                    <label class="form-check-label">
                                        <span class="badge bg-{{ $setting->value ? 'success' : 'secondary' }}">
                                            {{ $setting->value ? '启用' : '禁用' }}
                                        </span>
                                    </label>
                                </div>
                            @elseif($setting->type === 'text')
                                <div class="border rounded p-3 bg-light">
                                    <pre class="mb-0">{{ $setting->value }}</pre>
                                </div>
                            @elseif($setting->options && is_array($setting->options))
                                <div>
                                    <span class="badge bg-success">{{ $setting->options[$setting->value] ?? $setting->value }}</span>
                                    <small class="text-muted ms-2">({{ $setting->value }})</small>
                                </div>
                            @elseif(is_array($setting->value))
                                <div class="border rounded p-3 bg-light">
                                    <pre class="mb-0">{{ json_encode($setting->value, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) }}</pre>
                                </div>
                            @else
                                <code class="fs-6">{{ $setting->value }}</code>
                            @endif
                        </div>
                    </div>

                    @if($setting->options && is_array($setting->options))
                        <div class="mb-3">
                            <label class="form-label text-muted">可选项</label>
                            <div class="form-control-plaintext">
                                <div class="border rounded p-3 bg-light">
                                    @foreach($setting->options as $key => $value)
                                        <div class="d-flex justify-content-between align-items-center mb-1">
                                            <span><code>{{ $key }}</code></span>
                                            <span>{{ $value }}</span>
                                        </div>
                                    @endforeach
                                </div>
                            </div>
                        </div>
                    @endif

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label text-muted">排序权重</label>
                                <div class="form-control-plaintext">
                                    <span class="badge bg-light text-dark">{{ $setting->sort_order }}</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-md-4">
            <div class="card">
                <div class="card-header">
                    <h6 class="card-title mb-0">系统信息</h6>
                </div>
                <div class="card-body">
                    <table class="table table-sm">
                        <tr>
                            <td class="text-muted">设置ID</td>
                            <td><code>{{ $setting->id }}</code></td>
                        </tr>
                        <tr>
                            <td class="text-muted">创建时间</td>
                            <td>{{ $setting->created_at->format('Y-m-d H:i:s') }}</td>
                        </tr>
                        <tr>
                            <td class="text-muted">更新时间</td>
                            <td>{{ $setting->updated_at->format('Y-m-d H:i:s') }}</td>
                        </tr>
                        <tr>
                            <td class="text-muted">是否必填</td>
                            <td>
                                <span class="badge bg-{{ $setting->is_required ? 'danger' : 'secondary' }}">
                                    {{ $setting->is_required ? '是' : '否' }}
                                </span>
                            </td>
                        </tr>
                        <tr>
                            <td class="text-muted">是否公开</td>
                            <td>
                                <span class="badge bg-{{ $setting->is_public ? 'info' : 'secondary' }}">
                                    {{ $setting->is_public ? '是' : '否' }}
                                </span>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
            
            <div class="card mt-3">
                <div class="card-header">
                    <h6 class="card-title mb-0">快速操作</h6>
                </div>
                <div class="card-body">
                    <div class="d-grid gap-2">
                        <a href="{{ route('admin.system-settings.edit', $setting) }}" class="btn btn-primary btn-sm">
                            <i class="fas fa-edit"></i> 编辑设置
                        </a>
                        
                        @if($setting->type === 'boolean')
                            <form action="{{ route('admin.system-settings.update', $setting) }}" method="POST" class="d-inline">
                                @csrf
                                @method('PUT')
                                <input type="hidden" name="key" value="{{ $setting->key }}">
                                <input type="hidden" name="label" value="{{ $setting->label }}">
                                <input type="hidden" name="group" value="{{ $setting->group }}">
                                <input type="hidden" name="type" value="{{ $setting->type }}">
                                <input type="hidden" name="description" value="{{ $setting->description }}">
                                <input type="hidden" name="sort_order" value="{{ $setting->sort_order }}">
                                <input type="hidden" name="is_required" value="{{ $setting->is_required }}">
                                <input type="hidden" name="is_public" value="{{ $setting->is_public }}">
                                <input type="hidden" name="value" value="{{ $setting->value ? '0' : '1' }}">
                                <button type="submit" class="btn btn-{{ $setting->value ? 'warning' : 'success' }} btn-sm w-100">
                                    <i class="fas fa-toggle-{{ $setting->value ? 'off' : 'on' }}"></i> 
                                    {{ $setting->value ? '禁用' : '启用' }}
                                </button>
                            </form>
                        @endif
                        
                        <form action="{{ route('admin.system-settings.clear-cache') }}" method="POST" class="d-inline">
                            @csrf
                            <button type="submit" class="btn btn-outline-warning btn-sm w-100"
                                    onclick="return confirm('确定要清除缓存吗？')">
                                <i class="fas fa-trash"></i> 清除缓存
                            </button>
                        </form>
                        
                        <hr class="my-2">
                        
                        <form action="{{ route('admin.system-settings.destroy', $setting) }}" method="POST" 
                              onsubmit="return confirm('确定要删除这个设置吗？此操作不可恢复！')">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn btn-outline-danger btn-sm w-100">
                                <i class="fas fa-trash"></i> 删除设置
                            </button>
                        </form>
                    </div>
                </div>
            </div>
            
            <div class="card mt-3">
                <div class="card-header">
                    <h6 class="card-title mb-0">使用说明</h6>
                </div>
                <div class="card-body">
                    <div class="alert alert-info small mb-0">
                        <h6 class="alert-heading">在代码中使用：</h6>
                        <p class="mb-2">获取设置值：</p>
                        <code class="d-block mb-2">SystemSetting::get('{{ $setting->key }}')</code>
                        
                        <p class="mb-2 mt-3">设置值：</p>
                        <code class="d-block mb-0">SystemSetting::set('{{ $setting->key }}', $value)</code>
                        
                        @if($setting->is_public)
                            <p class="mb-2 mt-3">前端获取（公开设置）：</p>
                            <code class="d-block mb-0">SystemSetting::getPublicSettings()</code>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
